/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jugueteria_lm;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gorda
 */
public class Devoluciones extends javax.swing.JFrame {

    Conexion conectar = new Conexion();
    Jugueteria_Datos obj = new Jugueteria_Datos();

    public Devoluciones() {
        initComponents();
        MostrarDevolucion("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jModifica = new javax.swing.JMenuItem();
        jMElimina = new javax.swing.JMenuItem();
        jPDevol = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jTID = new javax.swing.JTextField();
        jTDañada = new javax.swing.JTextField();
        jTCodigo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jTBusca = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        jModifica.setText("Selecciona para modificar");
        jModifica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jModificaActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jModifica);

        jMElimina.setText("selecciona para eliminar registro");
        jMElimina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMEliminaActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMElimina);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPDevol.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.setComponentPopupMenu(jPopupMenu1);
        jScrollPane1.setViewportView(jTable1);

        jPDevol.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 79, 562, 145));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jPDevol.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 294, 562, 145));

        jButton1.setText("Mostrar venta");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 264, -1, -1));

        jTID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTIDActionPerformed(evt);
            }
        });
        jPDevol.add(jTID, new org.netbeans.lib.awtextra.AbsoluteConstraints(357, 51, 50, -1));

        jTDañada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTDañadaActionPerformed(evt);
            }
        });
        jPDevol.add(jTDañada, new org.netbeans.lib.awtextra.AbsoluteConstraints(425, 51, 45, -1));
        jPDevol.add(jTCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(493, 51, 53, -1));

        jLabel1.setBackground(new java.awt.Color(51, 51, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Codigo venta");
        jPDevol.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(493, 30, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("P. Dañada");
        jPDevol.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(425, 30, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("ID");
        jPDevol.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(383, 30, -1, -1));

        jButton2.setText("Nuevo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        jButton3.setText("Agregar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(79, 50, -1, -1));

        jButton4.setText("Actualizar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(156, 50, -1, -1));

        jButton5.setText("Buscar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, -1, -1));
        jPDevol.add(jTBusca, new org.netbeans.lib.awtextra.AbsoluteConstraints(101, 231, 58, -1));

        jButton6.setText("Mostrar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(243, 50, -1, -1));

        jButton7.setBackground(new java.awt.Color(255, 0, 51));
        jButton7.setText("Salir");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPDevol.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 450, 70, 30));

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jugueteria_lm/17.jpg"))); // NOI18N
        jPDevol.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 610, 510));

        getContentPane().add(jPDevol, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 1, 660, 520));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        try {
            try (PreparedStatement insertar = conectar.getConnection().prepareStatement("insert into devolucion(id_devol, parte_danada, devolucion) "
                            + " values(?,?,?)")) {
                insertar.setString(1, jTID.getText());
                insertar.setString(2, jTDañada.getText());
                insertar.setString(3, jTCodigo.getText());
                insertar.execute();
                insertar.close();
                JOptionPane.showMessageDialog(null, "Devolucion Agregado");
                MostrarDevolucion("");
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
         
        }


    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:

        try {

            PreparedStatement pre = conectar.getConnection().prepareStatement("UPDATE devolucion "
                    + "SET parte_danada='" + jTDañada.getText() + "', devolucion='" + jTCodigo.getText()
                    + "' WHERE id_devol='" + jTID.getText() + "';");
            pre.executeUpdate();

            JOptionPane.showMessageDialog(this, "Se Actualizo con exito");
            MostrarDevolucion("");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error al Corregir");
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    void MostrarDevolucion(String muestra) {
        DefaultTableModel ModeloDevolution = new DefaultTableModel();

        ModeloDevolution.addColumn("ID devolucion");
        ModeloDevolution.addColumn(" parte dañada");
        ModeloDevolution.addColumn("Codigo de venta");

        jTable1.setModel(ModeloDevolution);
        String sql = "";
        if (muestra.equals("")) {

            sql = "SELECT * FROM devolucion";
        } else {
            sql = "SELECT * FROM devolucion WHERE id_devol ='" + muestra + "'";
        }
        String[] datosDev = new String[3];
        try {
            Statement st = conectar.getConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                datosDev[0] = rs.getString(1);
                datosDev[1] = rs.getString(2);
                datosDev[2] = rs.getString(3);

                ModeloDevolution.addRow(datosDev);

            }
            jTable1.setModel(ModeloDevolution);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }


    }
    private void jTIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTIDActionPerformed

    private void jTDañadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTDañadaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTDañadaActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        jTID.setText(null);
        jTCodigo.setText(null);
        jTDañada.setText(null);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:

        MostrarDevolucion(jTBusca.getText());
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        MostrarDevolucion("");
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jModificaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jModificaActionPerformed
        // TODO add your handling code here:
        int fila2 = jTable1.getSelectedRow();
        if (fila2 >= 0) {
            jTID.setText(jTable1.getValueAt(fila2, 0).toString());
            jTDañada.setText(jTable1.getValueAt(fila2, 1).toString());
            jTCodigo.setText(jTable1.getValueAt(fila2, 2).toString());
            
        } else {
            JOptionPane.showMessageDialog(null, "Seleccionado una fila por favor");

        }
    }//GEN-LAST:event_jModificaActionPerformed

    private void jMEliminaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMEliminaActionPerformed
        // TODO add yotur handling code here:
        int fild = jTable1.getSelectedRow();
        if (fild > -1) {
            String id = String.valueOf(jTable1.getValueAt(fild, 0));
            obj.Elimina_Datos("devolucion", "id_devol", id);
            MostrarDevolucion("");
            fild = -1;
        }
    }//GEN-LAST:event_jMEliminaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        DefaultTableModel Modelodevo = new DefaultTableModel();

        Modelodevo.addColumn("ID ventas");
        Modelodevo.addColumn("cantidad juguetes");
        Modelodevo.addColumn("total");
        Modelodevo.addColumn("fecha");
        Modelodevo.addColumn("nombre juguete");
        Modelodevo.addColumn("empleado");
        Modelodevo.addColumn("nombre cliente");


        jTable2.setModel(Modelodevo);


        String[] datosDevoluciones = new String[7];
        try {
            Statement st = conectar.getConnection().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM ventas");
            while (rs.next()) {
                datosDevoluciones[0] = rs.getString(1);
                datosDevoluciones[1] = rs.getString(2);
                datosDevoluciones[2] = rs.getString(3);
                datosDevoluciones[3] = rs.getString(4);
                datosDevoluciones[4] = rs.getString(5);
                datosDevoluciones[5] = rs.getString(6);
                datosDevoluciones[6] = rs.getString(7);

                Modelodevo.addRow(datosDevoluciones);

            }
            jTable2.setModel(Modelodevo);
        } catch (SQLException ex) {
          JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
         this.setVisible(false);
    }//GEN-LAST:event_jButton7ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Devoluciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Devoluciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Devoluciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Devoluciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Devoluciones().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuItem jMElimina;
    private javax.swing.JMenuItem jModifica;
    private javax.swing.JPanel jPDevol;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTBusca;
    private javax.swing.JTextField jTCodigo;
    private javax.swing.JTextField jTDañada;
    private javax.swing.JTextField jTID;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
